package com.letoken.platform.app.controller;

import cn.hutool.core.util.NumberUtil;
import com.letoken.platform.app.service.AppUserAssetsService;
import com.letoken.platform.pub.annotation.auth.NotAuthorization;
import com.letoken.platform.pub.annotation.pub.Trim;
import com.letoken.platform.pub.constant.LetokenTokenConstant;
import com.letoken.platform.pub.enums.TokenTransactionTypeEnum;
import com.letoken.platform.pub.req.PageData;
import com.letoken.platform.pub.res.ApiResponse;
import com.letoken.platform.repo.app.req.token.QueryTokenAssetsHistoryReq;
import com.letoken.platform.repo.app.res.assets.QueryTokenAssetsPriceRes;
import com.letoken.platform.repo.app.res.assets.QueryTokenAssetsRes;
import com.letoken.platform.repo.app.res.assets.QueryTokenAssetsHistoryRes;
import com.letoken.platform.repo.common.CommDropDownRes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * @author letokenlabs
 * @date 2022/11/24$
 */
@RestController
@RequestMapping("/assets")
@Api(tags = "Le账户-资产", description = "Le账户用户资产")
public class AppUserAssetsController {


    @Autowired
    private AppUserAssetsService appUserAssetsService;

    @GetMapping("/token")
    @ApiOperation(value = "代币列表", notes = "查询Le账户钱包资产信息")
    public ApiResponse<List<QueryTokenAssetsRes>> queryTokenAssets() {
        return ApiResponse.create(appUserAssetsService.queryTokenAssets());
    }

    @GetMapping("/released_balance")
    @ApiOperation(value = "待释放金额let", notes = "待释放let")
    public ApiResponse<BigDecimal> queryReleasedBalance() {
        return ApiResponse.create(appUserAssetsService.queryReleasedBalance());
    }

    @Trim
    @GetMapping("/token/prices")
    @ApiOperation(value = "代币详情-价格历史", notes = "查询单个代币价格和曲线信息")
    public ApiResponse<List<QueryTokenAssetsPriceRes>> queryTokenAssetsPrices(
            @ApiParam(name = "name", value = "代币名称(LET/USDT)", required = true) @RequestParam String name) {
        return null;
    }

    @Trim
    @GetMapping("/token/transaction_type")
    @ApiOperation(value = "代币详情-历史交易类型（Tab）下拉框", notes = "查询单个代币价历史交易记录")
    @NotAuthorization
    public ApiResponse< List<CommDropDownRes>> queryTokenTransactionTypes(
            @ApiParam(name = "tokenId", value = "2", required = true) @RequestParam Integer tokenId) {

        // 交易类型(1充值/2提现/3发行/4周期流通/5分享奖/6生态建设奖)
        List<CommDropDownRes> res = new ArrayList<>();
        TokenTransactionTypeEnum[] typeEnums = TokenTransactionTypeEnum.values();
        for (int i = 0; i < typeEnums.length; i++) {
            if (typeEnums[i].getValue() >= 3 && !NumberUtil.equals(tokenId,LetokenTokenConstant.TOKEN_ID_LET)){
                break;
            }
            CommDropDownRes dropDown = new CommDropDownRes();
            dropDown.setId(typeEnums[i].getValue());
            dropDown.setName(typeEnums[i].getText());
            res.add(dropDown);
        }
        return ApiResponse.create(res);
    }

    @Trim
    @PostMapping("/token/history")
    @ApiOperation(value = "代币详情-交易记录", notes = "查询单个代币价历史交易记录")
    public ApiResponse<PageData<QueryTokenAssetsHistoryRes>> queryTokenAssetsHistory(@RequestBody @Validated QueryTokenAssetsHistoryReq req) {
        req.getPageReq().setSize(1000);
        return ApiResponse.create(appUserAssetsService.queryTokenAssetsHistory(req));
    }

}
